﻿//-----------------------------------------------------------------------
// <copyright file="MessageRouter.cs" company="Nathan Miller">
// Copyright (c) Nathan Miller.
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
//-----------------------------------------------------------------------
namespace TileTool.Message
{
    using System;

    /// <summary>
    /// A message router is a utility class which makes message registration,
    /// unregistration and sending with a particular message manager on a
    /// given channel easier by acting as a wrapper around the actual calls.
    /// </summary>
    public class MessageRouter
    {
        #region Fields - Private

        /// <summary>
        /// Message manager we will use.
        /// </summary>
        private IMessageManager msgManager;

        /// <summary>
        /// Message channel to use.
        /// </summary>
        private MessageChannel channel;

        #endregion

        #region Constructors

        /// <summary>
        /// Constructor for the message router.
        /// 
        /// If channel is null the global channel will be used.
        /// </summary>
        /// <param name="msgManager">Message manager to use.</param>
        /// <param name="channel">Message channel to send messages on.</param>
        public MessageRouter(IMessageManager msgManager, MessageChannel channel = null)
        {
            if (msgManager == null)
            {
                throw new ArgumentNullException("msgManager", "Message manager must be non-null.");
            }

            this.channel = channel;
            this.msgManager = msgManager;
        }

        #endregion

        #region Methods - Public

        /// <summary>
        /// Send a message on the passed channel.
        /// 
        /// If the channel is not the global channel, the message must be delivered to consumers listening to
        /// the channel as well as those listening to the global channel.
        /// </summary>
        /// <param name="msg">Message to send.</param>
        public void SendMessage(MessageBase msg)
        {
            this.msgManager.SendMessage(this.channel, msg);
        }

        /// <summary>
        /// Register a handler for the passed type of message.
        /// </summary>
        /// <typeparam name="T">Type of message to handle</typeparam>
        /// <param name="handler">Message handler to call when the message is received.</param>
        /// <param name="priority">Priority of the message handler.</param>
        /// <param name="filter">Filter to use for message filtering.</param>
        public void RegisterForMessage<T>(
            MessageHandler<T> handler,
            int priority = MessageHandlerPriority.Default,
            IMessageFilter filter = null) where T : MessageBase
        {
            this.msgManager.RegisterForMessage(this.channel, handler, priority, filter);
        }

        /// <summary>
        /// Register a handler for the passed type of message.
        /// </summary>
        /// <typeparam name="T">Type of message to handle</typeparam>
        /// <param name="handler">Message handler to call when the message is received.</param>
        /// <param name="filter">Filter to use for message filtering.</param>
        public void RegisterForMessage<T>(
            MessageHandler<T> handler,
            IMessageFilter filter) where T : MessageBase
        {
            this.msgManager.RegisterForMessage(this.channel, handler, MessageHandlerPriority.Default, filter);
        }

        /// <summary>
        /// Unregister a handler for the passed type of message.
        /// </summary>
        /// <typeparam name="T">Type of message to handle</typeparam>
        /// <param name="handler">Message handler to remove.</param>
        public void UnregisterForMessage<T>(MessageHandler<T> handler) where T : MessageBase
        {
            this.msgManager.UnregisterForMessage(this.channel, handler);
        }

        #endregion
    }
}
